// miniprogram/pages/admin/team/new/newUser.js
const app = getApp();
const util = require("../../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobList: ['摄影师', '客服', '后期', '化妆', '管理员'],
    user: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      if (options._id) {
        this.loadItem(options);
      }
    });
  },
  isEditing(e) {
    console.log('isEditing',e.detail)
    this.setData({
      isEditing: e.detail.isEditing
    })
  },
  loadItem: function (options) {
    const that = this;
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'get',
        table: 'team',
        where: {
          _id: options._id, studioid: app.globalData.user.studioid
        }
      },
      success: res => {
        if (res.result.data && res.result.data.length > 0){
          let user = res.result.data[0];
          that.setData({user: user});
          wx.setNavigationBarTitle({
            title: '修改同事信息',
          });
        }
      }
    });
  },


  /**
   * 保存数据
   */
  submitData: function (user) {
    wx.showLoading({
      title: '正在保存数据'
    });
    
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        table: 'team',
        op: 'update',
        data: user
      },
      success: res => {
        wx.cloud.callFunction({
          name: 'crud',
          data: {
            op: 'update',
            table: 'user',
            data: {_id: user.openid, nickName: user.nickName, name: user.name, phone: user.phone}
          },
          success: rr=>{
            wx.hideLoading();
            console.debug("Update result", res.result, rr.result);
            let pages = getCurrentPages();
            if (res.result._id || res.result.stats.updated) {
              if (pages.length > 1) {
                pages[pages.length - 2].onLoad();
              }
              wx.navigateBack();
            }
          }
        })
      },
      fail: err => {
        console.log(err);
        wx.hideLoading();
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      }
    })
  },

  apply: function (e) {
    let user = this.data.user;
    for (var n in e.detail.value) {
      user[n] = e.detail.value[n]
    }
    user.nodes =  this.selectComponent('#zEditor').data.nodes;
    if (!user._id) {
      if (app.globalData.user.studioid) {
        user.studioid = app.globalData.user.studioid;
      } else {
        wx.showModal({
          title: '提示',
          content: '您没有对应工作室，请创建或由主账号邀请加入',
          showCancel: false,
          success: r => {
            if (r.confirm) {
              wx.navigateTo({
                url: '../../studio/studio'
              })
            }
          }
        })
        return;
      }
      user.sales = 0;
    }

    user.updatetime = util.formatTime(new Date());
    this.submitData(user);
  },

  switchChange: function (e) {
    console.log(e.detail);
    if (e.target.id) {
      this.setData({
        [`user.${e.target.id}`]: this.data.jobList[e.detail.value]
      });
    } else {
      console.error("控件没有设置 id!")
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})